{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 600开头的股票是上证A股，属于大盘股\n",
    "# 600开头的股票是上证A股，属于大盘股，其中6006开头的股票是最早上市的股票，\n",
    "# 6016开头的股票为大盘蓝筹股；900开头的股票是上证B股；\n",
    "# 000开头的股票是深证A股，001、002开头的股票也都属于深证A股，\n",
    "# 其中002开头的股票是深证A股中小企业股票；\n",
    "# 200开头的股票是深证B股；\n",
    "# 300开头的股票是创业板股票；400开头的股票是三板市场股票。\n",
    "def stock_a(code):\n",
    "    # print(code)\n",
    "    # print(type(code))\n",
    "    # 上证A股  # 深证A股\n",
    "    if code.startswith('600') or code.startswith('6006') or code.startswith('601') or code.startswith('000') or code.startswith('001') or code.startswith('002'):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "# 过滤掉 st 股票。\n",
    "def stock_a_filter_st(name):\n",
    "    # print(code)\n",
    "    # print(type(code))\n",
    "    # 上证A股  # 深证A股\n",
    "    if name.find(\"ST\") == -1:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "# 过滤价格，如果没有基本上是退市了。\n",
    "def stock_a_filter_price(latest_price):\n",
    "    # float 在 pandas 里面判断 空。\n",
    "    if np.isnan(latest_price):\n",
    "        return False\n",
    "    else:\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      index    code   name  last_price  change_percent  change_amount  \\\n",
      "62       63  600666    奥瑞德        2.81           10.20           0.26   \n",
      "63       64  000608   阳光股份        3.03           10.18           0.28   \n",
      "64       65  002165  红 宝 丽        4.46           10.12           0.41   \n",
      "65       66  002652   扬子新材        3.59           10.12           0.33   \n",
      "66       67  002547   春兴精工        4.79           10.11           0.44   \n",
      "...     ...     ...    ...         ...             ...            ...   \n",
      "5638   5639  600193   创兴资源        4.16           -9.57          -0.44   \n",
      "5639   5640  000536   华映科技        8.06           -9.64          -0.86   \n",
      "5642   5643  600318   新力金融       10.98          -10.00          -1.22   \n",
      "5643   5644  002428   云南锗业       26.64          -10.00          -2.96   \n",
      "5644   5645  002416    爱施德       17.34          -10.02          -1.93   \n",
      "\n",
      "         volume      turnover  amplitude   high  ...  volume_ratio  \\\n",
      "62    2296712.0  6.376879e+08       7.06   2.81  ...          1.71   \n",
      "63    1372251.0  4.017102e+08       9.09   3.03  ...          2.72   \n",
      "64     769854.0  3.351966e+08      10.86   4.46  ...          3.08   \n",
      "65     857002.0  3.004028e+08      12.27   3.59  ...          3.22   \n",
      "66      95490.0  4.573971e+07       0.00   4.79  ...          0.24   \n",
      "...         ...           ...        ...    ...  ...           ...   \n",
      "5638  1176889.0  5.284086e+08      20.00   5.06  ...          1.81   \n",
      "5639  6889600.0  5.778044e+09       9.42   8.89  ...          1.55   \n",
      "5642  1828740.0  2.081195e+09       7.95  11.95  ...          1.34   \n",
      "5643  1669572.0  4.593200e+09       8.92  29.28  ...          1.09   \n",
      "5644  2739090.0  4.805043e+09       6.02  18.50  ...          1.99   \n",
      "\n",
      "      turnover_rate  pe_ratio  pb_ratio    market_cap  circulating_market_cap  \\\n",
      "62             9.49   -211.62      7.90  7.765471e+09            6.799506e+09   \n",
      "63            18.43    -84.77      0.94  2.272237e+09            2.256622e+09   \n",
      "64            10.58     53.36      1.63  3.279303e+09            3.245228e+09   \n",
      "65            16.75    113.90      6.77  1.838310e+09            1.836345e+09   \n",
      "66             0.87    -22.93     81.50  5.403394e+09            5.286925e+09   \n",
      "...             ...       ...       ...           ...                     ...   \n",
      "5638          27.67    -67.56      6.38  1.769552e+09            1.769552e+09   \n",
      "5639          24.93    -19.71     14.14  2.229422e+10            2.227045e+10   \n",
      "5642          35.67    125.76      5.24  5.629749e+09            5.629749e+09   \n",
      "5643          25.57    442.73     12.15  1.739912e+10            1.739687e+10   \n",
      "5644          22.38     30.70      3.59  2.148915e+10            2.122250e+10   \n",
      "\n",
      "      rise_speed  change_5min  change_ercent_60day  ytd_change_percent  \n",
      "62           0.0         0.00               112.88               32.55  \n",
      "63           0.0         0.00                98.04                5.21  \n",
      "64           0.0         0.00                27.43                7.99  \n",
      "65           0.0         0.00                20.47               12.54  \n",
      "66           0.0         0.00                91.60                2.57  \n",
      "...          ...          ...                  ...                 ...  \n",
      "5638         0.0        -0.24                22.71              -19.54  \n",
      "5639         0.0        -0.25               328.72              106.14  \n",
      "5642         0.0         0.00                74.84               56.41  \n",
      "5643         0.0         0.00               106.19              116.41  \n",
      "5644         0.0         0.00                83.30               95.05  \n",
      "\n",
      "[2341 rows x 23 columns]\n"
     ]
    }
   ],
   "source": [
    "from comm import *\n",
    "import akshare as ak\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "data = ak.stock_zh_a_spot_em()\n",
    "# print(data.index)\n",
    "# 解决ESP 小数问题。\n",
    "# data[\"esp\"] = data[\"esp\"].round(2)  # 数据保留2位小数\n",
    "data.columns = ['index', 'code', 'name', 'last_price', 'change_percent', 'change_amount',\n",
    "'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio',\n",
    "'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed',\n",
    "'change_5min', 'change_ercent_60day','ytd_change_percent']\n",
    "\n",
    "data = data.loc[data[\"code\"].apply(stock_a)].loc[data[\"name\"].apply(stock_a_filter_st)].loc[\n",
    "    data[\"last_price\"].apply(stock_a_filter_price)]\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "data-env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
